import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import GameConfig from "../../Script/GameSpecial/GameConfig";
import Action3dManager, { ActionMngType } from "../../Script/Common/Action3dManager";
import PlayerData from "../../Script/Common/PlayerData";
import GlobalPool from "../../Script/Common/GlobalPool";
import Loader from "../../Script/Common/Loader";
import GlobalData from "../../Script/Common/GlobalData";
// import { NATIVE_AD_STYLE } from "../../Script/Platform/SDK/HeyGameSDK/GlobalData";

const { ccclass, property } = cc._decorator;

@ccclass
export default class GameLobby extends yyComponent {
    /**场景/UI类型 */
    public get uiType() { return this._uiType; }
    protected _uiType = GlobalEnum.UI.lobby;

    public init() {
        this.initRoleLayer();
        // this.initBtnAudio();
        this.onEvents();
    }
    protected onEvents() {
        this.on(EventType.Common.DirectorEvent.chooseLevelItem, this.onChooseLevelItem, this);
        this.on(EventType.Common.UIEvent.exited, this.onExitUI, this);
    }

    public reset() {
        this.resetRoleLayer();
    }

    //是否已显示过互推弹窗
    protected dialogShowed: boolean = false;
    public show() {
        this.node.active = true;
        this.onEvents();
        this.playBtnStartGame();
        this.updateLevelNum();
        this.addRoleLayer();
        this.emit(EventType.Common.AudioEvent.playBGM, GlobalEnum.AudioClip.BGMLobby, true);
        this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.playerAssetBar);
    }
    public hide() {
        this.reset();
        this.node.active = false;
        this.offEvents();
    }

    //#region 音乐开关
    @property(cc.Sprite)
    protected btnAudio: cc.Sprite = null;
    @property(cc.SpriteFrame)
    protected audioOpen: cc.SpriteFrame = null;
    @property(cc.SpriteFrame)
    protected audioClose: cc.SpriteFrame = null;
    protected initBtnAudio() {
        this.btnAudio.node.on("touchend", this.onBtnAudio, this);
        let config = GameConfig.audioConfig.bgm;
        if (!!config) {
            this.btnAudio.spriteFrame = this.audioOpen;
        } else {
            this.btnAudio.spriteFrame = this.audioClose;
        }
    }
    protected onBtnAudio() {
        let config = GameConfig.audioConfig.bgm;
        config = !config;
        let audioConfig = {
            bgm: config,
            effect: config,
        };
        GameConfig.audioConfig = audioConfig;
        if (!!config) {
            this.btnAudio.spriteFrame = this.audioOpen;
            this.emit(EventType.Common.AudioEvent.resume);
        } else {
            this.btnAudio.spriteFrame = this.audioClose;
            this.emit(EventType.Common.AudioEvent.pause);
        }
    }
    //#endregion

    //#region 开始游戏按钮
    @property(cc.Node)
    protected btnStartGame: cc.Node = null;
    protected playBtnStartGame() {
        if (!this.btnStartGame) return;
        let s = 1;
        this.btnStartGame.setScale(s, s);
        s = 1.2;
        let scale1 = Action3dManager.scaleTo(1, s, s, s);
        scale1.easing(Action3dManager.easeSinInOut());
        s = 1;
        let scale2 = Action3dManager.scaleTo(1, s, s, s);
        scale2.easing(Action3dManager.easeSinInOut());
        let seq = Action3dManager.sequence(scale1, scale2);
        let repeat = Action3dManager.repeatForever(seq);
        Action3dManager.getMng(ActionMngType.UI).runAction(this.btnStartGame, repeat);
    }
    protected stopBtnStartGame() {
        if (!this.btnStartGame) return;
        Action3dManager.getMng(ActionMngType.UI).stopAllActions(this.btnStartGame);
    }
    protected onBtnStartGame() {
        this.resetRoleLayer();
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.startGame();
    }
    protected startGame() {
        let lv = this.chosedLevel;
        this.chosedLevel = null;
        this.emit(EventType.Common.DirectorEvent.enterLevel, lv);
    }
    //#endregion

    //#region 选择关卡
    protected chosedLevel: number = null;
    protected onBtnChooseLevel() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.chooseLevel);
    }
    protected onChooseLevelItem(level) {
        this.chosedLevel = level;
        this.startGame();
        this.emit(EventType.Common.UIEvent.exit, GlobalEnum.UI.chooseLevel);
    }
    //#endregion

    //#region 更多游戏按钮
    @property(cc.Node)
    protected btnMoreGame: cc.Node = null;
    protected updateBtnMoreGame() {
        if (!this.btnMoreGame) return;
        if (GameConfig[GlobalEnum.Common.GameFlowGlobalConfig.guoshen]) {
            this.btnMoreGame.active = false;
            //
        } else {
            this.btnMoreGame.active = true;
            //
        }
    }

    @property(cc.Node)
    protected moreGameTipIcon: cc.Node = null;
    protected playMoreGameTip() {
        if (!this.btnMoreGame || !this.btnMoreGame.active) return;
        if (!this.moreGameTipIcon) return;
        let s = 0.9;
        this.moreGameTipIcon.setScale(s, s, s);
        s = 1.1;
        let scale1 = Action3dManager.scaleTo(1, s, s, s);
        s = 0.9;
        let scale2 = Action3dManager.scaleTo(1, s, s, s);
        let rot1 = Action3dManager.rotateTo2d(0.1, 30);
        let rot2 = Action3dManager.rotateTo2d(0.8, 0);
        let delay = Action3dManager.delay(2);
        rot2.easing(Action3dManager.easeElasticOut());
        // let seq = Action3dManager.sequence(scale1, scale2);
        let seq = Action3dManager.sequence(delay, rot1, rot2);
        let repeat = Action3dManager.repeatForever(seq);
        Action3dManager.getMng(ActionMngType.UI).runAction(this.moreGameTipIcon, repeat);
    }
    protected stopMoreGameTip() {
        if (!this.btnMoreGame || !this.btnMoreGame.active) return;
        if (!this.moreGameTipIcon) return;
        Action3dManager.getMng(ActionMngType.UI).stopAllActions(this.moreGameTipIcon);
    }
    protected onBtnMoreGame() {
        // this.emit(EventType.Common.RecommendEvent.showLittlePage);
        // this.emit(EventType.Common.RecommendEvent.showBigPage);
        this.emit(EventType.Common.HeyGameSDK.showMoreGame);
    }
    //#endregion

    //#region 角色模型
    @property(cc.Node)
    protected roleLayer: cc.Node = null;
    protected initRoleLayer() { }
    protected resetRoleLayer() {
        GlobalPool.putAllChildren(this.roleLayer);
    }
    protected addRoleLayer() {
        let node = GlobalPool.get("ShopRole");
        if (!!node) {
            this.roleLayer.addChild(node);
        } else {
            Loader.loadBundle("Role", () => {
                Loader.loadBundleRes("Role", "Prefab/ShopRole", (res) => {
                    GlobalPool.createPool(res.data.name, res, res.data.name);
                    if (this.node.active && this.roleLayer.children.length == 0) {
                        let role = GlobalPool.get("ShopRole");
                        this.roleLayer.addChild(role);
                    }
                }, cc.Prefab, false);
            }, false, false);
        }
    }
    //#endregion

    //商城
    protected onBtnShop() {
        this.resetRoleLayer();
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.shop);
    }
    //抽奖
    protected onBtnSpin() {
        this.emit(EventType.Common.AudioEvent.playClickBtn);
        this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.spin);
    }

    //关卡
    @property(cc.Label)
    levelNum: cc.Label = null;
    updateLevelNum() {
        let level = PlayerData.getData("gameData.curLevel");
        this.levelNum.string = level;
    }

    protected onExitUI(ui) {
        if (ui == GlobalEnum.UI.shop) {
            this.addRoleLayer();
        }
    }

}
